﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace ARelogRelog
{
    public class Config
    {
        public static void AddARelogPath(string aRelogPath)
        {
            XmlDocument xdConfig = Load();

            XmlElement xe = xdConfig.CreateElement("arelog");
            xe.SetAttribute("path", aRelogPath);
            xdConfig.DocumentElement.AppendChild(xe);

            Save(xdConfig);
        }

        public static void RemoveRelogPath(int index)
        {
            XmlDocument xdConfig = Load();

            xdConfig.DocumentElement.RemoveChild(xdConfig.DocumentElement.ChildNodes[index]);

            Save(xdConfig);
        }

        public static List<Schedule> LoadARelogSchedules(ARelog pARelog)
        {
            XmlDocument xdConfig = Load();
            XmlElement xeARelog = (XmlElement)xdConfig.DocumentElement.SelectSingleNode("arelog[@path = '" + pARelog.FileName + "']");

            List<Schedule> list = new List<Schedule>();
            foreach (XmlElement xe in xeARelog.SelectNodes("schedule"))
            {
                string[] start = xe.GetAttribute("start").Split(':');
                string[] end = xe.GetAttribute("end").Split(':');

                list.Add(new Schedule(
                    new DateTime(1753, 1, 1, Convert.ToInt32(start[0]), Convert.ToInt32(start[1]), Convert.ToInt32(start[2])),
                    new DateTime(1753, 1, 1, Convert.ToInt32(end[0]), Convert.ToInt32(end[1]), Convert.ToInt32(end[2]))));
            }

            return list;
        }

        public static void SaveARelogSchedules(ARelog pARelog)
        {
            XmlDocument xdConfig = Load();
            XmlElement xeARelog = (XmlElement)xdConfig.DocumentElement.SelectSingleNode("arelog[@path = '" + pARelog.FileName + "']");

            foreach (XmlElement xeSchedule in xeARelog.SelectNodes("schedule"))
                xeARelog.RemoveChild(xeSchedule);

            foreach (Schedule pSchedule in pARelog.Schedules)
            {
                XmlElement xe = xdConfig.CreateElement("schedule");
                xeARelog.AppendChild(xe);

                xe.SetAttribute("start", string.Format("{0}:{1}:{2}",
                    pSchedule.Start.Hour,
                    pSchedule.Start.Minute,
                    pSchedule.Start.Second));

                xe.SetAttribute("end", string.Format("{0}:{1}:{2}",
                    pSchedule.End.Hour,
                    pSchedule.End.Minute,
                    pSchedule.End.Second));
            }

            Save(xdConfig);
        }

        public static List<ARelog> ARelogs
        {
            get
            {
                XmlDocument xd = Load();

                List<ARelog> pList = new List<ARelog>();
                foreach (XmlElement xe in xd.DocumentElement.ChildNodes)
                {
                    pList.Add(new ARelog(xe.GetAttribute("path")));
                }

                return pList;
            }
        }

        static XmlDocument Load()
        {
            XmlDocument xd = new XmlDocument();

            if (!File.Exists("config.xml"))
            {
                xd.AppendChild(xd.CreateElement("config"));
                xd.Save("config.xml");
            }
            else
            {
                xd.Load("config.xml");
            }

            return xd;
        }

        static void Save(XmlDocument xd)
        {
            xd.Save("config.xml");
        }
    }
}
